
package com.smartfit;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.graphics.Color;

import com.charts.entity.DateValueEntity;
import com.charts.entity.IStickEntity;
import com.charts.entity.LineEntity;
import com.charts.entity.OHLCEntity;
import com.charts.view.GridChart;
import com.charts.view.LineChart;

public class ChartLine
{
	List<IStickEntity> ohlc_x;
	List<IStickEntity> ohlc_y;
	List<IStickEntity> ohlc_z;
	
	int ohlc_size = 0;
	
	LineChart m_LineChart;
	
	ChartLine( LineChart lineChart )
	{
		m_LineChart = lineChart;

		this.ohlc_x = new ArrayList<IStickEntity>();
		this.ohlc_y = new ArrayList<IStickEntity>();
		this.ohlc_z = new ArrayList<IStickEntity>();
		
		// 데이터 초기화
		Initialize();

		// 차트 생성
		GenerateChart();
	}
	
	public void Initialize()
	{
		this.ohlc_x.clear();
		this.ohlc_y.clear();
		this.ohlc_z.clear();
		
		ohlc_size = 0;
		
		/*
		for (int i = 0; i < 100; i++) {
			
			//double x_val = randInt(1,102);
			int precision  = 1;
			double x_val = 0.15569391448032208 * precision * randInt(1,10000);
			double y_val = -0.1124882805264601* precision * randInt(1,10000);
			double z_val = 0.14204034619509825* precision *randInt(1,10000);

			ohlc_x.add(new OHLCEntity(x_val,x_val,x_val,x_val, 20130117));
			ohlc_y.add(new OHLCEntity(y_val,y_val,y_val,y_val, 20130717));
			ohlc_z.add(new OHLCEntity(z_val,z_val,z_val,z_val, 20130717));
		}
		
		ohlc_size = ohlc_x.size();

		ReDrawChart();
		/**/
		
		Add( 0 , 0.0 , 0.0 , 0.0 );		
	}
	
	private void GenerateChart()
	{
		List<LineEntity<DateValueEntity>> lines = new ArrayList<LineEntity<DateValueEntity>>();

		//X value 
		LineEntity<DateValueEntity> MA1 = new LineEntity<DateValueEntity>();
		MA1.setTitle("X");
		MA1.setLineColor(Color.RED);
		MA1.setLineData(initMA(2,this.ohlc_x));
		lines.add(MA1);
		
		
		// Y value
		LineEntity<DateValueEntity> MA10 = new LineEntity<DateValueEntity>();
		MA10.setTitle("Y");
		MA10.setLineColor(Color.GREEN);
		MA10.setLineData(initMA(2,this.ohlc_y));
		lines.add(MA10);

		// Z value
		LineEntity<DateValueEntity> MA25 = new LineEntity<DateValueEntity>();
		MA25.setTitle("Z");
		MA25.setLineColor(Color.BLUE);
		MA25.setLineData(initMA(2,this.ohlc_z));
		lines.add(MA25);

		
		//m_LineChart.refreshDrawableState();
		m_LineChart.setAxisXColor(Color.LTGRAY);
		m_LineChart.setAxisYColor(Color.LTGRAY);
		m_LineChart.setBorderColor(Color.LTGRAY);
		m_LineChart.setLongitudeFontSize(14);
		m_LineChart.setLongitudeFontColor(Color.WHITE);
		m_LineChart.setLatitudeColor(Color.GRAY);
		m_LineChart.setLatitudeFontColor(Color.WHITE);
		m_LineChart.setLongitudeColor(Color.GRAY);
		m_LineChart.setMaxValue(3);
		m_LineChart.setMinValue(-3);
		
		//m_LineChart.setMaxPointNum(36);
		m_LineChart.setMaxPointNum( ohlc_size );
		m_LineChart.setDisplayLongitudeTitle(true);
		m_LineChart.setDisplayLatitudeTitle(true);
		m_LineChart.setDisplayLatitude(true);
		m_LineChart.setDisplayLongitude(true);
		m_LineChart.setLatitudeNum(10);
		m_LineChart.setLongitudeNum(10);
		m_LineChart.setDataQuadrantPaddingTop(5);
		m_LineChart.setDataQuadrantPaddingBottom(5);
		m_LineChart.setDataQuadrantPaddingLeft(5);
		m_LineChart.setDataQuadrantPaddingRight(5);
		m_LineChart.setAxisYTitleQuadrantWidth(50);
		m_LineChart.setAxisXTitleQuadrantHeight(20);
		m_LineChart.setAxisXPosition(GridChart.AXIS_X_POSITION_BOTTOM);
		m_LineChart.setAxisYPosition(GridChart.AXIS_Y_POSITION_RIGHT);

		// 为chart1增加均线
		m_LineChart.setLinesData(lines);
	}

	public void ReDrawChart()
	{
		this.m_LineChart.invalidate();
		//this.m_LineChart.setMaxPointNum( ohlc_size );
		GenerateChart();
	}
	
	public void Add( int i , double x , double y , double z )
	{
		ohlc_x.add( new OHLCEntity( x , x , x , x , 20130000 + i ) );
		ohlc_y.add( new OHLCEntity( y , y , y , y , 20130000 + i ) );
		ohlc_z.add( new OHLCEntity( z , z , z , z , 20130000 + i ) );
		
		ohlc_size = ohlc_x.size();
		
//		ReDrawChart();
	}
	
	private List<DateValueEntity> initMA( int days , List<IStickEntity> ohlc )
	{
		if ( days < 2 )
		{
			return null;
		}

		List<DateValueEntity> MA5Values = new ArrayList<DateValueEntity>();

		float sum = 0;
		float avg = 0;
		
//		int iStart = ( ( ohlc.size() > 100 ) ? ohlc.size() - 100 : 0 );
		
		for ( int i = 0 ; i < ohlc.size() ; i++ )
		{
			float close = ( float )( ( OHLCEntity )ohlc.get(i) ).getClose();
			
			if ( i < days )
			{
				sum = sum + close;
				avg = sum / ( i + 1f );
			}
			else
			{
				sum = sum + close - ( float )( ( OHLCEntity )ohlc.get( i - days ) ).getClose();
				avg = sum / days;
			}
			
			MA5Values.add( new DateValueEntity( avg , ohlc.get( i ).getDate() ) );
		}

		return MA5Values;
	}	

	public static int randInt( int min , int max )
	{

	    // NOTE: Usually this should be a field rather than a method
	    // variable so that it is not re-seeded every call.
	    Random rand = new Random();

	    // nextInt is normally exclusive of the top value,
	    // so add 1 to make it inclusive
	    int randomNum = rand.nextInt((max - min) + 1) + min;

	    return randomNum;
	}
}